package ui.widget;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import ui.util.YPUIUtil;

public class TitleRetractablePanel extends YPanel {
	private TitlePanel titlePanel;
	protected JPanel mainPanel;
	private String title;
	public TitleRetractablePanel() {
		titlePanel = new TitlePanel();
		init();
	}
	public TitleRetractablePanel(String title) {
		this.title = title;
		titlePanel = new TitlePanel(title);
		init();
	}
	
	public TitleRetractablePanel(int w,int h,String title) {
		titlePanel = new TitlePanel(w,h,title);
		init();
	}
	
	private void init() {
		mainPanel = new YPanel();
		add(titlePanel);
		titlePanel.setPanelClickedEvent((e)->{
			if(titlePanel.isSelected()) {
				remove(mainPanel);
				titlePanel.setSelected(false);
			}else {
				add(mainPanel);
				titlePanel.setSelected(true);
			}
			validate();
			repaint();
			revalidate();
		});
	}
	
	public JPanel getMainPanel() {
		return mainPanel;
	}
	public void setMainPanel(JPanel mainPanel) {
		this.mainPanel = mainPanel;
	}
	public void setTitle(String title) {
		titlePanel.setTxt(title);
	}
	
	public String getTitle() {
		return titlePanel.getTxt();
	}

	private class TitlePanel extends ColorXTransparentPanel{
		String txt;
		private TxtPanel txtPanel;
		public TitlePanel() {
			init();
		}
		public TitlePanel(int w,int h,String txt) {
			super(w,h);
			this.txt=txt;
			init();
		}
		public TitlePanel(String txt) {
			this(YPUIUtil.getScreenWidth(),30,txt);
		}
		
		private void init() {
			txtPanel = new TxtPanel(YPUIUtil.getScreenWidth(),30,"      "+txt);
			add(txtPanel);
		}

		public String getTxt() {
			return txt;
		}

		public void setTxt(String txt) {
			this.txt = txt;
			txtPanel.setText(txt);
		}

		@Override
		protected void childRepaintAfterMe(Graphics g) {
			Graphics2D g2d = (Graphics2D) g.create();
			int h = getHeight();
			int w = getWidth();
			int x1 = 5, y1 = 10, x2 = 10, y2 = 15, x3 = 5, y3 = 20;
			int x4 = 5, y4 = 10, x5 = 10, y5 = 15, x6 = 15, y6 = 10;
			if (isSelected()) {
				g2d.drawLine(x4, y4, x5, y5);
				g2d.drawLine(x5, y5, x6, y6);
				g2d.drawLine(x4, y4 + 5, x5, y5 + 5);
				g2d.drawLine(x5, y5 + 5, x6, y6 + 5);
			} else {
				g2d.drawLine(x1, y1, x2, y2);
				g2d.drawLine(x3, y3, x2, y2);
				g2d.drawLine(x1 + 5, y1, x2 + 5, y2);
				g2d.drawLine(x3 + 5, y3, x2 + 5, y2);
			}
		}
		
	}
}
